function httpRequest(url, json, on_success, on_fail) {
    let request = new XMLHttpRequest();
    request.onreadystatechange = () => {
        if (request.readyState === 4) {
            if (request.status === 200 && on_success != null) {
                on_success(request.responseText);
            } else {
                if (on_fail != null)
                    on_fail(request.status);
            }
        }
    };

    request.open("post", url, true);
    request.setRequestHeader("Content-Type", "application/json");
    request.send(json);
}

function urlReplace(url) {
    const queryString = window.location.search.substring(1);
    if (queryString.length > 0)
        url = url + "?" + queryString;
    window.location.replace(url);
}

function isNull(string) {
    return string === "" || string == null;
}

function logout() {
    httpRequest("http://172.27.71.125:7402/leave/api/logout", JSON.stringify({"token": sessionStorage.getItem("token")}));
    sessionStorage.setItem("token", "");
    urlReplace("login.html");
}
